import requests
import mysql.connector

def pa():

    #学校名称
    schoolName = "西安交通大学"

    #爬取链接
    url = "http://job.xjtu.edu.cn/xsfw/sys/jyxtgktapp/modules/jywzManage/getMhcxWzData.do?requestParamStr={%22pageSize%22%3A200%2C%22pageNumber%22%3A1%2C%22LMDM%22%3A%229164e68abb88443d869cf75c663156ef%22}"

    #请求头
    headers = {

    }
    #爬取数据并解析
    html = requests.get(url=url, headers=headers)

    #解析JSON
    dataList = html.json().get("data")

    for data in dataList:

        print(data.get("menutitle")+"|"+data.get("SBSJ"))
        mysql_conn(data.get("menutitle"),schoolName,data.get("SBSJ"))


#数据库连接
def mysql_conn(job_title,school_name,create_time):
    mydb = mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        passwd="",
        database="hschool_job"
    )
    mycursor = mydb.cursor()

    #插入
    sql = "INSERT INTO h_job (job_title,school_name,create_time) VALUES (%s,%s,%s)"
    val = (job_title,school_name,create_time)
    mycursor.execute(sql, val)
    mydb.commit()

if __name__ == '__main__':
    pa()
